﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace GetHistory
{
    public partial class HistoryData : UserControl
    {
        public HistoryData()
        {
            InitializeComponent();
        }

        public DataSet Data 
        {
            get
            { return this.dataSet; } 
        }

        public void PopulateHistory(WorkItem wi)
        {
            dataSet.Clear();
            try
            {
                this.Cursor = Cursors.WaitCursor;

                InitializeDataSet(wi.Revisions.Count);
                DataRow row;

                foreach (Revision r in wi.Revisions)
                {
                    for (int i = 0; i < r.Fields.Count; i++)
                    {
                        if (!Properties.Settings.Default.Ignore.Contains(r.Fields[i].Name))
                        {
                            if (!string.IsNullOrEmpty(string.Format("{0}", r.Fields[i].Value)))
                            {
                                //Populate the DataGrid
                                DataRow[] foundRows;
                                foundRows = dataSet.Tables["Revision"].Select("Field = '" + r.Fields[i].Name + "'");
                                if (foundRows.Length == 0)
                                {
                                    row = dataSet.Tables["Revision"].NewRow();
                                    row["Field"] = r.Fields[i].Name;
                                    row["Rev " + r.Index.ToString()] = r.Fields[i].Value;
                                    dataSet.Tables["Revision"].Rows.Add(row);
                                }
                                else
                                {
                                    row = foundRows[0];
                                    row["Rev " + r.Index.ToString()] = r.Fields[i].Value;
                                }
                            }
                        }
                    }
                }
                //excelExportButton.Enabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show(string.Format("Work Item {0} not found", wi.Id.ToString()));
            }
            finally
            {
                this.Cursor = Cursors.Default;
                historydataGridView.DataSource = dataSet.Tables["Revision"];
            }
        }

        private void InitializeDataSet(int revisionCount)
        {
            dataSet = new DataSet();
            DataTable table = new DataTable("Revision");
            table.Columns.Add("Field", typeof(string));
            table.Columns["Field"].Unique = true;
            for (int i = 0; i < revisionCount; i++)
            {
                table.Columns.Add("Rev " + i.ToString(), typeof(string));
            }
            dataSet.Tables.Add(table);
        }

        private void historydataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex > 1)
            {
                if (e.Value.ToString() != historydataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString())
                    e.CellStyle.ForeColor = Color.Red;
            }
        }
    }
}
